package com.atguigu.service.impl;

import com.atguigu.entity.EduChapter;
import com.atguigu.entity.EduVideo;
import com.atguigu.entity.chapter.ChapterVo;
import com.atguigu.entity.chapter.VideoVo;
import com.atguigu.exceptionhandler.GuliException;
import com.atguigu.mapper.EduChapterMapper;
import com.atguigu.service.EduChapterService;
import com.atguigu.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author wxc
 * @since 2021-07-04
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    //注入小节service
    @Autowired
    private EduVideoService videoService;

    /**
     * 课程大纲列表，根据课程id进行查询
     *
     * @param courseId
     **/
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //1、根据课程id，查询课程里面所有的章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        //2、根据课程id，查询课程里面所有的小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId);
        wrapperVideo.orderByAsc("sort");
        List<EduVideo> eduVideoList = videoService.list(wrapperVideo);

        //创建list集合，用于最终封装数据
        List<ChapterVo> list = new ArrayList<>();

        //3、遍历查询章节list集合进行封装
        for (EduChapter chapter : eduChapterList) {
            ChapterVo chapterVo = new ChapterVo();
            //将第一个对象的值赋给第二个对象
            BeanUtils.copyProperties(chapter, chapterVo);


            //创建集合，用于封装章节小节
            List<VideoVo> videoVoList = new ArrayList<>();

            //4、遍历查询小节list集合，进行封装
            for (EduVideo video : eduVideoList) {
                if(chapter.getId().equals(video.getChapterId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video, videoVo);
                    System.out.println("--------------------------");
                    System.out.println("video = " + video);
                    System.out.println("videoVo = " + videoVo);
                    videoVoList.add(videoVo);
                }
            }

            //将小节list封装到章节中
            chapterVo.setChildren(videoVoList);

            list.add(chapterVo);

        }



        return list;
    }

    /**
     * 删除章节
     *
     * @param chapterId
     **/
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据章节id查询小节表，如果查询数据，不进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = videoService.count(wrapper);
        //判断
        if(count > 0){  //有小节，不进行删除
            throw new GuliException(20001, "不能删除");
        }else{          //没有小节，可以删除
            int i = baseMapper.deleteById(chapterId);
            return i > 0;
        }
    }

    /**
     * 根据课程id删除章节
     *
     * @param courseId
     **/
    @Override
    public void removeChapterBYCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }
}
